package com.minyifang.wcspf.work.ui.home.activities;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.minyifang.wcspf.R;
import com.minyifang.wcspf.common.base.BaseMvpActivity;
import com.minyifang.wcspf.common.utils.LogUtil;
import com.minyifang.wcspf.common.utils.ToastHelper;
import com.minyifang.wcspf.work.common.MyWebViewActivity;
import com.minyifang.wcspf.work.model.HttpRespond;
import com.minyifang.wcspf.work.ui.home.model.NewsBean;
import com.minyifang.wcspf.work.ui.home.model.NewsDetailsBean;
import com.minyifang.wcspf.work.ui.home.presenter.NewsListPresenter;
import com.minyifang.wcspf.work.ui.home.view.NewsListView;
import com.minyifang.wcspf.work.ui.user.model.ProtocolBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class NewsListActivity extends BaseMvpActivity<NewsListView, NewsListPresenter> implements NewsListView {


    @BindView(R.id.rv_news)
    RecyclerView mRvNews;

    private NewsListAdapter adapter;
    private List<NewsBean> list;

    @Override
    protected NewsListPresenter createPresenter() {
        return new NewsListPresenter();
    }

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_news_list;
    }

    @Override
    protected void initView() {

        list = new ArrayList<>();
        adapter = new NewsListAdapter(R.layout.item_news_list, list);
        mRvNews.setAdapter(adapter);
        mRvNews.setLayoutManager(new LinearLayoutManager(this));

        adapter.setOnItemClickListener((adapter, view, position) -> mPresenter.getNewsDetails(list.get(position).getID()));
    }

    @Override
    protected void initData() {
        mPresenter.getNewsList();
    }

    @Override
    public void showLoadingView() {

    }

    @Override
    public void hideLoadingView() {

    }

    @Override
    public void onNetworkError() {

    }

    @Override
    public void getNewsListRes(HttpRespond<List<NewsBean>> respond) {
        LogUtil.e(respond.toString());
        if (respond.result == 1) {
            list.addAll(respond.data);
            adapter.notifyDataSetChanged();
        } else
            ToastHelper.getInstance(this).showWarn(respond.message);
    }

    @Override
    public void showNewsDetails(NewsDetailsBean data) {
        startActivity(MyWebViewActivity.getIntent(this, data.getTitle(), data.getContents()));
    }

    @Override
    public void showRequestFailed(String message) {
        ToastHelper.getInstance(this).showWarn(message);

    }

    class NewsListAdapter extends BaseQuickAdapter<NewsBean, BaseViewHolder> {

        public NewsListAdapter(int layoutResId, List<NewsBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, NewsBean item) {
            helper.setText(R.id.tv_title, item.getTitle());
        }
    }

}
